<?php

class User
{

    public function __construct(){
        $this->session = get_lib('session');
    }

    /** 判断用户是否有该url权限  暂时废弃
     * @param $user_id
     * @param $controller_model
     * @return bool
     */
    public function hasPermission($user_id, $controller_model){

        return true;
        $path_info = pathinfo($controller_model);

        $admin_model = get_model('admin_model');
        $user_info = $admin_model->j_row(['admin_id'=>$this->session->admin_id]);

        $role_model = get_model('Role_model');
        $role_info = $role_model->j_row(['id'=> $user_info['role_id']]);

        $arr = $this->db->from('admin')->where(['admin_id'=>$this->session->admin_id])->select('authorization')->get()->row_array();

        explode($controller_model);

        if($Controller_model != '/'){
			$authorization = explode(',', $arr['authorization']);
			$auth = $this->db->from('con_met')->where_in('id',$authorization)->get()->result_array();
			foreach ($auth as $k => $v) {
				//echo strtolower($Controller_model).'|';

				$res = '/'.$v['controller'].'/'.$v['method'];
				//echo strtolower($res);die;
				if(strstr(strtolower($Controller_model),strtolower($res))){
					//echo 1;die;
					$val = 1;
				}
			}
			if($val != 1){
				// echo '没有权限';die;
				redirect('login/auth');
			}
		}


//        if(strstr(strtolower($Controller_model),strtolower($res))){
//            //echo 1;die;
//            $val = 1;
//        }

        return true;
    }


//    public function get_login_user(){
//        return $this->session->admin_id;
//    }

    public function get_user_id(){
        return $this->session->admin_id;
    }

    public function getPermission(){
        $user_id = $this->get_user_id();
        return $this->get_permission_by_user($user_id);
    }

    public function isAdmin(){
        $admin_model = get_model('admin_model');
        $user_info = $admin_model->j_row(['admin_id' => $this->session->admin_id]);

        if($user_info['is_admin'] == '1'){
            return true;
        }else{
            return false;
        }
    }


    /** 根据用户来获取他的权限
     * @param $user_id
     * @return array
     */
    public function get_permission_by_user($user_id){
        $admin_model = get_model('admin_model');
        $user_info = $admin_model->j_row(['admin_id' => $user_id]);

        $role_model = get_model('Role_model');
        $role_info = $role_model->j_row(['id'=> $user_info['role_id']]);

        return explode(',', $role_info['permission']);
    }



    public function get_login_user_info(){
        $user_id = $this->get_user_id();
        $admin_model = get_model('admin_model');
        $user_info = $admin_model->j_row(['admin_id' => $user_id]);

        $user_info['style_type'] = $user_info['style_type'] ? unserialize($user_info['style_type']) : [];
        return $user_info;
    }

    
    
}
